"""
集合

Version: 0.1
Author: yuwl
Date：2024-1-1
"""


# 创建集合
# 创建集合的字面量语法
set1 = {1, 2, 3, 3, 3, 2}
print(set1) # {1, 2, 3}
print('Length = ', len(set1)) # Length =  3
# 创建集合的构造器语法
set2 = set(range(1, 10))
set3 = set((1, 2, 3, 3, 2, 1))
print(set2, set3) # {1, 2, 3, 4, 5, 6, 7, 8, 9} {1, 2, 3}
# 创建集合的推导式语法
set4 = {num for num in range(1, 100) if num % 3 == 0 or num % 5 == 0}
print(set4)


# 向集合添加元素
set1.add(4)
set1.add(5)
print(set1) # {1, 2, 3, 4, 5}
# update更新集合
set2.update([11, 12]) 
print(set2) # {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12}
# discard移除集合中的指定元素，如果元素不存在也不会引发错误
set2.discard(5)
print(set2) # {1, 2, 3, 4, 6, 7, 8, 9, 11, 12}
# remove移除集合中的指定元素，如果元素不存在则会引发错误
if 4 in set2:
	set2.remove(4) 
print(set2) # {1, 2, 3, 6, 7, 8, 9, 11, 12}
# pop随机移除并返回集合中的一个元素
print(set3.pop()) # 1
print(set3) # {2, 3}
# clear()：移除集合中的所有元素
set4.clear()
print(set4) # set()


# 集合的成员、交集、并集、差集、对称差运算
print(set1) # {1, 2, 3, 4, 5}
print(set2) # {1, 2, 3, 6, 7, 8, 9, 11, 12}
print(set3) # {2, 3}
# 交集
print(set1 & set2) # {1, 2, 3}
print(set1.intersection(set2)) # {1, 2, 3}
# 并集
print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12}
print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12}
# 差集
print(set1 - set2) # {4, 5}
print(set1.difference(set2)) # {4, 5}
# 对称差
print(set1 ^ set2) # {4, 5, 6, 7, 8, 9, 11, 12}
print(set1.symmetric_difference(set2)) # {4, 5, 6, 7, 8, 9, 11, 12}
# 判断子集和超集
print(set2 <= set1) # False
print(set2.issubset(set1)) # False
print(set3 <= set1) # True
print(set3.issubset(set1)) # True
print(set1 >= set2) # False
print(set1.issuperset(set2)) # False
print(set1 >= set3) # True
print(set1.issuperset(set3)) # True